﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using SortOf;

namespace ExampleHash
{
    public partial class Form1 : Form
    {

        string[] names = new string[9999];
        string name;


        public Form1()
        {
            InitializeComponent();
        }

        static int SimpleHash(string s, string[] arr)
        {
            int tot = 0;
            char[] cname;
            cname = s.ToCharArray();
            for (int i = 0; i <= cname.GetUpperBound(0); i++)
            {
                tot += (int)cname[i];
            }
            return tot % arr.GetUpperBound(0);
        }

        static int BetterHash(string s, string[] arr)
        {
            long tot = 0;
            char[] cname;
            cname = s.ToCharArray();

            for (int i = 0; i <= cname.GetUpperBound(0); i++)
			{
			    tot += 37 * tot + (int) cname[i];
			}

            tot = tot % arr.GetUpperBound(0);

            if(tot < 0)
            {
                tot += arr.GetUpperBound(0);
            }

            return (int) tot;
        }


        static void ShowDistrib(string[] arr)
        {
            for (int i = 0; i <= arr.GetUpperBound(0); i++)
            {
                if (arr[i] != null)
                {
                    MessageBox.Show(i + " " + arr[i]);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] someNames = new string[] { "David", "Roy", "Justin", "Tedjan", "Dennis", "Melvin", "Tim" };
            int hashVal;
            for (int i = 0; i < 7; i++)
            {
                name = someNames[i];
                hashVal = BetterHash(name, names);
                names[hashVal] = name;
            }
            ShowDistrib(names);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BucketHash hash = new BucketHash();

            hash.Insert("Roy");
            hash.Insert("Roy1");
            hash.Insert("Roy2");

            MessageBox.Show(hash.View());

            hash.Remove("Roy1");

            MessageBox.Show(hash.View());
        }
    }
}
